//
//  SEOnlineReserveImageCell.swift
//  SmartEyeForPatient
//
//  Created by QDSG on 2021/4/7.
//

import UIKit

protocol SEOnlineReserveImageCellDelegate: NSObjectProtocol {
    func imageCell(_ cell: SEOnlineReserveImageCell, toImagePickerWith viewModel: SEInquiryInfoViewModel)
    func imageCell(_ cell: SEOnlineReserveImageCell, deleteImageWith viewModel: SEInquiryInfoViewModel)
}

extension SEOnlineReserveImageCellDelegate {
    func imageCell(_ cell: SEOnlineReserveImageCell, deleteImageWith viewModel: SEInquiryInfoViewModel) {}
}

class SEOnlineReserveImageCell: UITableViewCell {
    
    weak var delegate: SEOnlineReserveImageCellDelegate?
    
    private var viewModel: SEInquiryInfoViewModel?
    
    override func awakeFromNib() {
        super.awakeFromNib()
        backgroundColor = .background
        contentView.backgroundColor = .background
    }

    func update(with viewModel: SEInquiryInfoViewModel) {
        frame = UIScreen.main.bounds
        layoutIfNeeded()
        
        self.viewModel = viewModel
        collectionView.reloadData()
        
        let contentSize = collectionView.collectionViewLayout.collectionViewContentSize
        collectionViewHeightConstraint.constant = contentSize.height
    }
    
    @IBOutlet private var collectionViewHeightConstraint: NSLayoutConstraint!
    
    @IBOutlet weak var collectionView: UICollectionView! {
        didSet {
            collectionView.backgroundColor = .background
            collectionView.delaysContentTouches = false
            collectionView.delegate = self
            collectionView.dataSource = self
            collectionView.register(SEImageCell.nibForCell(),
                                    forCellWithReuseIdentifier: SEImageCell.reuseIdentifier())
        }
    }
}

// MARK: - UICollectionViewDataSource

extension SEOnlineReserveImageCell: UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return viewModel?.numberOfRowsForImageItem(isCanEdit: true) ?? 1
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: SEImageCell.reuseIdentifier(), for: indexPath) as! SEImageCell
        if let viewModel = viewModel {
            cell.update(with: viewModel, indexPath: indexPath, isCanEdit: true) { (vm) in
                self.delegate?.imageCell(self, deleteImageWith: vm)
            }
        }
        return cell
    }
}

// MARK: - UICollectionViewDelegate & UICollectionViewDelegateFlowLayout

extension SEOnlineReserveImageCell: UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let itemWH = (UIScreen.screenWidth - 15 * 4) / 3
        return CGSize(width: itemWH, height: itemWH)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 15
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 15
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        collectionView.deselectItem(at: indexPath, animated: true)
        
        guard let viewModel = viewModel else { return }
        
        if viewModel.isAddImageCell(isCanEdit: true, at: indexPath) {
            delegate?.imageCell(self, toImagePickerWith: viewModel)
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, shouldHighlightItemAt indexPath: IndexPath) -> Bool {
        return true
    }
    
    func collectionView(_ collectionView: UICollectionView, didHighlightItemAt indexPath: IndexPath) {
        let cell = collectionView.cellForItem(at: indexPath) as? SEImageCell
        cell?.backgroundColor = .background
    }
    
    func collectionView(_ collectionView: UICollectionView, didUnhighlightItemAt indexPath: IndexPath) {
        let cell = collectionView.cellForItem(at: indexPath) as? SEImageCell
        cell?.backgroundColor = .clear
    }
}
